package com.lanou.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.Course;
import com.lanou.entity.CourseQuery;
import com.lanou.mapper.CourseMapper;
import com.lanou.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO
 * @Author cuipengwei
 * @Date 2025年08月13日 11:10
 * @Version 1.0
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    //查询全部
    @Override
    public List<Course> selectAll() {
        List<Course> courses = courseMapper.selectAll();
        return courses;
    }
    //分页多条件查询
    @Override
    public PageInfo<Course> selectAllByName(Course course, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courses = courseMapper.selectAllByName(course);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return pageInfo;
    }
    //增加
    @Override
    public Integer addCourse(Course course) {
        int i = courseMapper.addCourse(course);
        return i;
    }
    //删除
    @Override
    public Integer deleteCourse(Integer id) {
        Integer i = courseMapper.deleteCourse(id);
        return i;
    }
    //修改
    @Override
    public Integer updateCourse(Course course) {
        Integer i = courseMapper.updateCourse(course);
        return i;
    }
    //根据id查询
    @Override
    public Course getCourseId(Integer id) {
        Course courseId = courseMapper.getCourseId(id);
        return courseId;
    }

    @Override
    public PageInfo<Course> selectCourseBySort(String sortType,String interval,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CourseQuery courseQuery = new CourseQuery();
        String priceInterval[];
        System.out.println(interval);
        Double minPrice=null;
        Double maxPrice=null;
        if(sortType!=null&&!sortType.isEmpty()&&!"null".equals(sortType)){
            courseQuery.setSortType(sortType);
        }
        if(interval!=null&&!interval.isEmpty()&&!"null".equals(interval)){
             priceInterval = interval.split("-");
             minPrice=Double.valueOf(priceInterval[0]);
             maxPrice=Double.valueOf(priceInterval[1]);
             courseQuery.setMinPrice(minPrice);
             courseQuery.setMaxPrice(maxPrice);

            System.out.println(minPrice+" "+maxPrice+"5555");
        }



        List<Course> courses = courseMapper.selectCourseBySort(courseQuery);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return pageInfo;
    }


}
